﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using Microsoft.Practices.Unity;
using BugTracker.BusinessLogic.Project;
using BugTracker.WebUX.WebCode;
using BugTacker.Common;

namespace BugTracker.WebUX.Modules.Shared
{
    public partial class UCOptions : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
                if (BTSession.ProjectID.Value == 0)
                {
                    ProjectDropDown_OnSelectedIndexChanged(ProjectDropDown, EventArgs.Empty);
                }
            } 
        }

        private void BindData()
        {
            ProjectDropDown.DataSource = Global.IoC.Container.Resolve<IProjectService>().Load();
            ProjectDropDown.DataBind();
        }

        protected void ProjectDropDown_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            int id = 0;
            Int32.TryParse(ProjectDropDown.SelectedValue.ToString(), out id);
            BTSession.ProjectID = id;
        }

        private void FindProjectID()
        {
            var projectId = BTSession.ProjectID;
            if (projectId.HasValue & projectId != 0)
            {
                CommonRoutines.DropDownSelectByValue(ProjectDropDown, projectId.Value);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.FindProjectID();
        }
    }
}